﻿using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace TelerikKeyGen {


    /// <summary>
    /// This Generator used for Telerik Q3-2006. Just set correct DomainIP or DomainName (ex: jbngan.com).
    /// 
    /// </summary>
    public class TelerikKeyGenerator_Q3_2006 {

        private string ArrayString;
        private string _domainIP = "127.0.0.1";
        private string _domainName = String.Empty;
        private string _companyName = String.Empty;
        private static TelerikKeyGenerator_Q3_2006 _instance = new TelerikKeyGenerator_Q3_2006();
        private string[] _controlNames;

        public static TelerikKeyGenerator_Q3_2006 Instance {
            get { return _instance; }
        }

        public string DomainIP {
            get { return _domainIP; }
            set { _domainIP = value; }
        }

        public string DomainName {
            get { return _domainName; }
            set { _domainName = value; }
        }

        public string CompanyName {
            get { return _companyName; }
            set { _companyName = value; }
        }

        public string GetControlName(int Index) {
            return _controlNames[Index];
        }

        public int ControlCount {
            get { return _controlNames.Length; }
        }

        private TelerikKeyGenerator_Q3_2006() {
            this.ArrayString = "";
            this.ArrayString = this.ArrayString + "118,99,120,111,105,117,72,71,109,100,51,55,56,49,70,68|56,57,118,99,109,101,53,103,102,109,71,72,109,115,100,80/";
            this.ArrayString = this.ArrayString + "117,101,85,55,51,110,77,111,67,100,49,75,109,77,116,84|111,87,53,52,118,68,83,54,49,48,110,72,70,56,113,65/";
            this.ArrayString = this.ArrayString + "116,69,120,52,111,85,107,55,49,122,88,122,67,118,51,49|110,78,48,79,102,102,68,101,83,119,87,116,51,50,57,74/";
            this.ArrayString = this.ArrayString + "85,85,121,82,118,49,57,55,51,53,120,67,118,107,75,104|84,114,116,105,79,112,50,57,98,118,69,100,99,106,72,121/";
            this.ArrayString = this.ArrayString + "105,111,114,101,67,77,119,115,106,104,53,52,65,100,100,120|57,48,53,52,115,118,70,68,70,100,109,107,101,119,51,70/";
            this.ArrayString = this.ArrayString + "70,70,111,51,57,90,49,109,74,103,99,81,51,55,71,53|110,104,67,56,49,72,76,76,111,80,104,86,119,115,88,57/";
            this.ArrayString = this.ArrayString + "89,116,70,103,50,115,79,112,76,104,104,71,103,103,116,82|110,110,66,118,67,102,51,57,50,48,120,122,122,114,82,119/";
            this.ArrayString = this.ArrayString + "70,103,76,113,50,57,49,86,120,119,80,107,106,103,66,115|77,77,48,120,120,67,122,81,112,76,54,106,69,116,107,111/";
            this.ArrayString = this.ArrayString + "48,49,118,111,105,82,116,72,110,115,121,85,99,86,113,49|49,48,55,116,52,99,86,101,51,98,110,77,105,107,74,111/";
            this.ArrayString = this.ArrayString + "105,121,117,56,55,50,48,107,76,109,78,98,86,103,70,116|107,102,69,101,119,113,53,50,107,110,99,68,100,111,49,80/";
            this.ArrayString = this.ArrayString + "72,70,69,67,100,66,101,53,57,88,119,49,48,76,109,112|105,110,118,88,119,111,81,49,48,53,57,50,121,72,56,52/";
            this.ArrayString = this.ArrayString + "105,111,120,57,51,50,88,100,115,106,110,109,57,48,51,97|56,52,105,106,102,100,104,51,55,50,83,68,117,51,76,102/";
            this.ArrayString = this.ArrayString + "110,86,99,49,48,120,90,115,68,111,87,113,112,111,89,116|82,114,82,116,85,105,48,57,50,122,50,54,66,110,77,77/";
            this.ArrayString = this.ArrayString + "105,117,82,103,55,51,88,115,87,112,76,76,49,50,86,98|107,67,120,83,119,113,51,52,111,85,121,84,98,72,106,107/";
            this.ArrayString = this.ArrayString + "117,72,110,98,81,111,105,68,120,57,50,51,55,54,90,120|115,100,57,56,54,53,57,72,100,110,109,118,99,103,72,52/";
            this.ArrayString = this.ArrayString + "110,118,101,105,70,71,55,116,114,117,49,48,57,50,56,106|105,111,102,100,117,57,52,51,68,109,104,49,52,98,118,76/";
            this.ArrayString = this.ArrayString + "111,111,116,114,67,121,50,55,109,112,102,118,103,80,114,118|120,120,71,110,109,113,49,48,111,109,110,79,102,106,76,74/";
            this.ArrayString = this.ArrayString + "105,117,107,55,54,55,77,107,104,106,103,57,54,100,118,103|105,57,55,56,121,106,107,85,105,72,74,111,54,55,111,79";

            this._controlNames = new string[] { "panelbar", "chart", "spell", "dock", "treeview", "rotator", "window", "toolbar", "input", "calendar", "combobox", "grid", "tabstrip", "editor", "ajax", "menu", "upload", "splitter" };
        }

        public string GetKey(int Index) {
            byte[] buffer = null;
            int index = 0;
            int num2 = 0;
            int num3 = 0;
            string[] sourceArray = new string[] { _domainIP, _companyName, "forever", _domainName, "v2", "" };
            char[] separator = new char[] { '/' };
            char[] chArray2 = new char[] { '|' };
            char[] chArray3 = new char[] { ',' };
            string[] textArray3 = this.ArrayString.Split(separator)[Index].Split(chArray2)[0].Split(chArray3);
            string[] textArray4 = null;

            chArray3 = new char[] { '/' };
            chArray2 = new char[] { '|' };
            separator = new char[] { ',' };
            textArray4 = this.ArrayString.Split(chArray3)[Index].Split(chArray2)[1].Split(separator);

            byte[] buffer2 = new byte[(textArray3.Length - 1) + 1];
            buffer = new byte[(textArray4.Length - 1) + 1];
            int num4 = buffer2.Length - 1;
            for (index = 0; index <= num4; index++) {
                buffer2[index] = Convert.ToByte(textArray3[index]);
            }
            num3 = buffer.Length - 1;
            for (num2 = 0; num2 <= num3; num2++) {
                buffer[num2] = Convert.ToByte(textArray4[num2]);
            }
            string s = String.Join(Convert.ToChar(1).ToString(), sourceArray);
            return string.Format("<license control=\"{0}\" company=\"{1}\" licenseKey=\"{2}\" />",
                _controlNames[Index], sourceArray[1].ToString(),
                this.GenerateKey(Encoding.ASCII.GetBytes(s), buffer2, buffer));
        }

        private string GenerateKey(byte[] lic, byte[] key1, byte[] key2) {
            RijndaelManaged rmInstance = new RijndaelManaged();
            rmInstance.Mode = CipherMode.CBC;
            MemoryStream obj3 = new MemoryStream();

            ICryptoTransform icTransform = rmInstance.CreateEncryptor(key1, key2);
            CryptoStream stream = new CryptoStream(obj3, icTransform, CryptoStreamMode.Write);
            stream.Write(lic, 0, lic.Length);
            stream.Close();

            obj3.Close();
            rmInstance.Clear();
            return Convert.ToBase64String(obj3.ToArray());
        }

        /// <summary>
        /// Save License File
        /// </summary>
        /// <param name="Filename"></param>
        /// <param name="Index">Specify control index or -1 for all</param>
        public void SaveLicenseFile(string Filename, int Index) {
            string text = "<root>\r\n";
            if (Index == -1) {
                for (int i = 0; i < _controlNames.Length; i++) {
                    text += " " + this.GetKey(i) + "\r\n";
                }
            } else {
                text += this.GetKey(Index) + "\r\n";
            }
            text += "</root>";
            System.IO.File.WriteAllText(Filename, text);
        }

        public void SaveLicenseFile(string Filename, string[] Names) {
            string text = "<root>\r\n";
            for (int i = 0; i < Names.Length; i++) {
                int Index = Array.IndexOf(_controlNames, Names[i]);
                if (Index != -1)
                    text += " " + this.GetKey(Index) + "\r\n";
            }
            text += "</root>";
            System.IO.File.WriteAllText(Filename, text);
        }

    }
}


